package GUI;

import ModelLayer.*;
import CtrlLayer.*;

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class OrdersGUI extends JFrame implements ActionListener{
    private Container container;
    private JPanel panelOrders;
    private JPanel panelStartOrder;
    private JPanel panelShowOrder;
    private JPanel panelShowOrder2;
    JLabel labelProduct;
    JLabel errorMessage;
    JTextField textBarcode;
    JTextField textQuantity;
    private int count;
    private int sessionId;
    private int orderId;
    private OrderController orderController;

    public OrdersGUI(int sessionId){
        this.sessionId = sessionId;
        orderId = 0;
        count = 0;
        this.setTitle("Vestbjerg company system");
        this.setLocation(300,50);
        this.setVisible(true);
        this.setSize(400, 470);
        this.setResizable(false);
        panelOrders = new JPanel(new GridBagLayout());
        panelStartOrder = new JPanel(new GridBagLayout());
        panelShowOrder = new JPanel(new GridBagLayout());
        panelShowOrder2 = new JPanel(new GridBagLayout());
        orderController = new OrderController();
        makeOrders();
    }

    public void actionPerformed(ActionEvent e){

    }

    public void makeMenuOrders(){
        GridBagConstraints gbc = new GridBagConstraints();

        JButton addOrder = new JButton("Start an Order");
        addOrder.setPreferredSize(new Dimension(150, 25));
        addOrder.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    startOrder();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelOrders.add(addOrder, gbc);

        JButton showOrders = new JButton("Show an Order");
        showOrders.setPreferredSize(new Dimension(150, 25));
        showOrders.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    findOrder();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelOrders.add(showOrders, gbc);

        JButton back = new JButton("Close");
        back.setPreferredSize(new Dimension(150, 25));
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    dispose();
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelOrders.add(back, gbc);
    }

    public void makeOrders(){
        makeMenuOrders();
        this.getContentPane().add(panelOrders, BorderLayout.NORTH);
    }

    private void startOrderMenu(){
        JTextField textContractorId = new JTextField();
        JLabel labelContractorId;
        JLabel labelBarcode;
        JLabel labelQuantity;
        JButton buttonAdd;
        JButton buttonRemove;
        JButton buttonSubmit;
        JButton buttonBack;
        JButton buttonClear;
        GridBagConstraints gbc = new GridBagConstraints();

        labelContractorId = new JLabel("Customer ID:", JLabel.RIGHT);
        labelContractorId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelStartOrder.add(labelContractorId, gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textContractorId.setPreferredSize(new Dimension(100,25));
        panelStartOrder.add(textContractorId,gbc);

        labelBarcode = new JLabel("Barcode:", JLabel.LEFT);
        labelBarcode.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelStartOrder.add(labelBarcode, gbc);

        labelQuantity = new JLabel("Quantity:", JLabel.LEFT);
        labelQuantity.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelStartOrder.add(labelQuantity, gbc);

        labelProduct = new JLabel("Product 1:", JLabel.RIGHT);
        labelProduct.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelStartOrder.add(labelProduct, gbc);

        textBarcode = new JTextField();
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textBarcode.setPreferredSize(new Dimension(100,25));
        panelStartOrder.add(textBarcode,gbc);

        textQuantity = new JTextField();
        gbc.gridx = 2;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textQuantity.setPreferredSize(new Dimension(100,25));
        panelStartOrder.add(textQuantity,gbc);

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelStartOrder.add(errorMessage, gbc);

        buttonAdd = new JButton("Add more");
        buttonRemove = new JButton("Remove");
        buttonSubmit = new JButton("Submit");
        buttonBack = new JButton("Back");
        buttonClear = new JButton("Clear");

        buttonAdd.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    if(textBarcode.getText().equals("") || textQuantity.getText().equals("")){
                        errorMessage.setText("Not possible!");
                        buttonAdd.setEnabled(false);
                        buttonRemove.setEnabled(false);
                        buttonSubmit.setEnabled(false);
                        buttonBack.setEnabled(false);
                    }else{

                        if (orderId==0)
                            orderId = orderController.createOrder(sessionId);
                        String barcode = textBarcode.getText();
                        int productBarcode = Integer.parseInt(barcode);
                        int amount = Integer.parseInt(textQuantity.getText());
                        boolean check = orderController.addProductInOrderManually(orderId, productBarcode, amount);
                        if(!check) {
                            orderController.removeProductFromOrderManually(orderId,productBarcode,amount);
                        }

                        count++;
                        labelProduct = new JLabel("Product " + (count+1) + ":", JLabel.RIGHT);
                        labelProduct.setPreferredSize(new Dimension(100, 25));
                        gbc.gridx = 0;
                        gbc.gridy = 3 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        panelStartOrder.add(labelProduct, gbc);

                        textBarcode = new JTextField();
                        gbc.gridx = 1;
                        gbc.gridy = 3 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        textBarcode.setPreferredSize(new Dimension(100,25));
                        panelStartOrder.add(textBarcode,gbc);

                        textQuantity = new JTextField();
                        gbc.gridx = 2;
                        gbc.gridy = 3 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        textQuantity.setPreferredSize(new Dimension(100,25));
                        panelStartOrder.add(textQuantity,gbc);

                        errorMessage = new JLabel("", JLabel.RIGHT);
                        errorMessage.setPreferredSize(new Dimension(100, 25));
                        gbc.gridx = 0;
                        gbc.gridy = 4 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        panelStartOrder.add(errorMessage, gbc);

                        gbc.gridx = 0;
                        gbc.gridy = 5 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        buttonAdd.setPreferredSize(new Dimension(100,25));
                        panelStartOrder.add(buttonAdd,gbc);

                        gbc.gridx = 1;
                        gbc.gridy = 5 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        buttonRemove.setPreferredSize(new Dimension(100,25));
                        panelStartOrder.add(buttonRemove,gbc);

                        gbc.gridx = 2;
                        gbc.gridy = 5 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        buttonSubmit.setPreferredSize(new Dimension(100,25));
                        panelStartOrder.add(buttonSubmit,gbc);

                        gbc.gridx = 1;
                        gbc.gridy = 6 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        buttonClear.setPreferredSize(new Dimension(100,25));
                        panelStartOrder.add(buttonClear,gbc);

                        gbc.gridx = 2;
                        gbc.gridy = 6 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        buttonBack.setPreferredSize(new Dimension(100,25));
                        panelStartOrder.add(buttonBack,gbc);

                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        buttonAdd.setPreferredSize(new Dimension(100,25));
        panelStartOrder.add(buttonAdd,gbc);

        buttonRemove.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){

                    //  orderController.removeProductFromOrder(orderId,Integer.parseInt(textBarcode),Integer.parseInt(textQuantity));

                    container = getContentPane();
                    panelStartOrder.remove(labelProduct);
                    panelStartOrder.remove(textBarcode);
                    panelStartOrder.remove(textQuantity);
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    count--;

                    gbc.gridx = 0;
                    gbc.gridy = 5 + count;
                    gbc.insets = new Insets(5,5,5,5);
                    buttonAdd.setPreferredSize(new Dimension(100,25));
                    panelStartOrder.add(buttonAdd,gbc);

                    gbc.gridx = 1;
                    gbc.gridy = 5 + count;
                    gbc.insets = new Insets(5,5,5,5);
                    buttonRemove.setPreferredSize(new Dimension(100,25));
                    panelStartOrder.add(buttonRemove,gbc);

                    gbc.gridx = 2;
                    gbc.gridy = 5 + count;
                    gbc.insets = new Insets(5,5,5,5);
                    buttonSubmit.setPreferredSize(new Dimension(100,25));
                    panelStartOrder.add(buttonSubmit,gbc);

                    gbc.gridx = 1;
                    gbc.gridy = 6 + count;
                    gbc.insets = new Insets(5,5,5,5);
                    buttonClear.setPreferredSize(new Dimension(100,25));
                    panelStartOrder.add(buttonClear,gbc);

                    gbc.gridx = 2;
                    gbc.gridy = 6 + count;
                    gbc.insets = new Insets(5,5,5,5);
                    buttonBack.setPreferredSize(new Dimension(100,25));
                    panelStartOrder.add(buttonBack,gbc);
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        buttonRemove.setPreferredSize(new Dimension(100,25));
        panelStartOrder.add(buttonRemove,gbc);

        buttonSubmit.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){

                    if (orderId==0)
                        orderId = orderController.createOrder(sessionId);
                    String barcode = textBarcode.getText();
                    int productBarcode = Integer.parseInt(barcode);
                    int amount = Integer.parseInt(textQuantity.getText());
                    boolean check = orderController.addProductInOrderManually(orderId, productBarcode, amount);
                    if(!check) {
                        orderController.removeProductFromOrderManually(orderId,productBarcode,amount);
                    }

                    orderController.endOrder(orderId);
                    orderId=0;
                    count = 0;
                    container = getContentPane();
                    panelStartOrder.removeAll();
                    container.removeAll();
                    makeOrders();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);

                }
            }
        );
        gbc.gridx = 2;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        buttonSubmit.setPreferredSize(new Dimension(100,25));
        panelStartOrder.add(buttonSubmit,gbc);

        buttonClear.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    textBarcode.setText("");
                    textQuantity.setText("");
                    buttonAdd.setEnabled(true);
                    buttonRemove.setEnabled(true);
                    buttonSubmit.setEnabled(true);
                    buttonBack.setEnabled(true);
                    container = getContentPane();
                    panelStartOrder.removeAll();
                    container.removeAll();
                    makeOrders();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    count = 0;
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        buttonClear.setPreferredSize(new Dimension(100,25));
        panelStartOrder.add(buttonClear,gbc);

        buttonBack.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    panelStartOrder.removeAll();
                    container.removeAll();
                    makeOrders();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    count = 0;
                }
            }
        );
        gbc.gridx = 2;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        buttonBack.setPreferredSize(new Dimension(100,25));
        panelStartOrder.add(buttonBack,gbc);
    }

    public void startOrder(){
        startOrderMenu();
        this.getContentPane().add(panelStartOrder, BorderLayout.NORTH);
    }
    
    private void menuShowOrder(){
        JTextField textId = new JTextField();
        JLabel labelId;
        JLabel errorMessage;
        JButton find;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setText("");

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelShowOrder.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelShowOrder.add(textId,gbc);

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelShowOrder.add(errorMessage, gbc);

        find = new JButton("Search");
        find.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        container = getContentPane();
                        container.removeAll();
                        findOrderSecondWindow(Integer.parseInt(textId.getText()));
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        textId.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex){
                        container = getContentPane();
                        container.removeAll();
                        findOrder();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        errorMessage.setText("No order!");
                        textId.setText("");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        find.setPreferredSize(new Dimension(100,25));
        panelShowOrder.add(find,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    textId.setText("");
                    errorMessage.setText("");

                    container = getContentPane();
                    panelShowOrder.removeAll();
                    container.removeAll();
                    makeOrders();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelShowOrder.add(back,gbc);
    }

    public void findOrder()
    {
        menuShowOrder();
        this.getContentPane().add(panelShowOrder, BorderLayout.NORTH);
    }

    public void findSecondWindow(int id){
        JTextField textId = new JTextField();
        JTextField textDate = new JTextField();
        JTextField textEmployee = new JTextField();
        JLabel labelId;
        JLabel labelDate;
        JLabel labelBarcode;
        JLabel labelQuantity;
        JLabel labelNull;
        JLabel labelEmployee;
        JButton ok;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setEditable(false);
        textDate.setEditable(false);
        textEmployee.setEditable(false);

        textId.setText(Integer.toString(orderController.findOrder(id).getId()));
        textDate.setText(orderController.findOrder(id).getDate());
        textEmployee.setText(Integer.toString(orderController.findOrder(id).getEmployeeID()));

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelShowOrder2.add(labelId, gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelShowOrder2.add(textId,gbc);

        labelDate = new JLabel("Date:", JLabel.RIGHT);
        labelDate.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelShowOrder2.add(labelDate, gbc);

        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textDate.setPreferredSize(new Dimension(100,25));
        panelShowOrder2.add(textDate,gbc);
        
        labelEmployee = new JLabel("Employee:", JLabel.RIGHT);
        labelEmployee.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelShowOrder2.add(labelEmployee, gbc);

        gbc.gridx = 2;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textEmployee.setPreferredSize(new Dimension(100,25));
        panelShowOrder2.add(textEmployee,gbc);

        labelBarcode = new JLabel("Barcode:", JLabel.LEFT);
        labelBarcode.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelShowOrder2.add(labelBarcode, gbc);

        labelQuantity = new JLabel("Quantity:", JLabel.LEFT);
        labelQuantity.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 2;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelShowOrder2.add(labelQuantity, gbc);
        
        int y = 5;
        for(int i=0; i<OrderContainer.getInstance().findOrderById(id).getProducts().size(); i++){

            int productNumber = i;
            labelProduct = new JLabel("Product "+ ++productNumber +":", JLabel.RIGHT);
            labelProduct.setPreferredSize(new Dimension(100, 25));
            gbc.gridx = 0;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            panelShowOrder2.add(labelProduct, gbc);

            textBarcode = new JTextField();
            gbc.gridx = 1;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textBarcode.setPreferredSize(new Dimension(100,25));
            panelShowOrder2.add(textBarcode,gbc);

            textQuantity = new JTextField();
            gbc.gridx = 2;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textQuantity.setPreferredSize(new Dimension(100,25));
            panelShowOrder2.add(textQuantity,gbc);
            y++;

            textBarcode.setEditable(false);
            textQuantity.setEditable(false);
            int key = (int) OrderContainer.getInstance().findOrderById(id).getProducts().keySet().toArray()[i];
            textBarcode.setText(Integer.toString(key));
            textQuantity.setText(Integer.toString(orderController.findOrder(id).getProducts().get(key)));
        }

        labelNull = new JLabel("", JLabel.RIGHT);
        labelNull.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 2;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelShowOrder2.add(labelNull, gbc);

        ok = new JButton("OK");
        ok.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    findOrder();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        ok.setPreferredSize(new Dimension(100,25));
        panelShowOrder2.add(ok,gbc);
    }

    public void findOrderSecondWindow(int id)
    {
        findSecondWindow(id);
        this.getContentPane().add(panelShowOrder2, BorderLayout.NORTH);
    }
}
